package org.yyf.resource.demo.annotation.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.anuyun.ms.common.BusinessException;
import com.anuyun.ms.common.Result;


@Aspect
public class HandleExceptionAspect {
	
	private final Logger log = LoggerFactory.getLogger(this.getClass());

	/**
	 * 注解切点
	 */
	@Pointcut("@annotation(com.cn.yyf.annotation.HandleException)")
	public void point() {
	}

	@Around("point()")
	public Object serviceAOP(ProceedingJoinPoint pJoinPoint) {
		
		try {
			return pJoinPoint.proceed();
		} catch (Throwable e) {
			if (e instanceof BusinessException) {
				log.info("业务异常: " + e.getMessage());
				return new Result<>(((BusinessException) e).getCode(), e.getMessage());
			} else {
				log.error("系统异常: ", e);
				return new Result<>(1, "系统错误: " + e.getMessage());
			}
		}

	}

}
